import request from './_base'

export function apiGetArticlesPublic(id) {
  return request({
    url: `/api/v1/articles/public/${id}`,
    method: 'get'
  })
}

export function apiGetArticles(page, size, tagName, keyword) {
  return request({
    url: '/api/v1/articles',
    method: 'get',
    params: {
      page,
      size,
      tag_name: tagName,
      keyword
    }
  })
}

export function apiGetArticleDetail(id) {
  return request({
    url: `/api/v1/articles/detail/${id}`,
    method: 'get'
  })
}

export function apiGetArticleTag(id) {
  return request({
    url: `/api/v1/articles/tag/${id}`,
    method: 'get'
  })
}

export function apiGetArticleTree() {
  return request({
    url: '/api/v1/articles/tree',
    method: 'get'
  })
}

export function apiPutArticle(id, data) {
  return request({
    url: `/api/v1/articles/${id}`,
    method: 'put',
    data
  })
}

export function apiPostArticle(title, tag_name, status) {
  return request({
    url: '/api/v1/articles',
    method: 'post',
    data: { title, tag_name, status }
  })
}

export function apiPutArticleTitle(id, title) {
  return request({
    url: `/api/v1/articles/title/${id}`,
    method: 'put',
    data: { title }
  })
}

export function apiPutArticleContent(id, content) {
  return request({
    url: `/api/v1/articles/content/${id}`,
    method: 'put',
    data: { content }
  })
}

export function apiPutArticleTag(id, tag_name) {
  return request({
    url: `/api/v1/articles/tag/${id}`,
    method: 'put',
    data: { tag_name }
  })
}

export function apiPutArticleStatus(id, status) {
  return request({
    url: `/api/v1/articles/status/${id}`,
    method: 'put',
    data: { status }
  })
}

export function apiDeleteArticle(id) {
  return request({
    url: `/api/v1/articles/${id}`,
    method: 'delete'
  })
}

export function apiGetTags() {
  return request({
    url: '/api/v1/tags',
    method: 'get'
  })
}

export function apiPutTags(id, name) {
  return request({
    url: `/api/v1/tags/${id}`,
    method: 'put',
    data: { name }
  })
}

export function apiDeleteTags(id) {
  return request({
    url: `/api/v1/tags/${id}`,
    method: 'delete'
  })
}

export function apiPostImage(file, articleId) {
  const param = new FormData()
  param.append('img', file)
  param.append('article_id', articleId)
  const config = {
    headers: { 'Content-Type': 'multipart/form-data' }
  }
  return request.post('/api/v1/files/images', param, config)
}
